package com.dotstone.chipism.bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;

import com.dotstone.chipism.util.ConvertUtil;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.SocketManager;
//import org.json.JSONException;
//import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class MySocket {

	private String tcpIp;
	private int tcpPort;
	private boolean localSuccess = false;
	private boolean remoteSuccess = false;// 是否远程连接成功
	private boolean isStopByUser = false;
	private boolean stop = false;
	private String mac = "";

	private String manufacturer = "";
	private String productId = "";
	private String deviceId = "";
	private int status;
	private Context c;
	private Socket socket_local;
	private InputStream in_local;// 本地socket输入流
	private OutputStream out_local;// 本地socket输出流
	// private boolean receiving = false;
	// private StringBuffer sb = new StringBuffer();
	private RecDataCallBackListener_Bind listener_Bind;
	private RecDataCallBackListener_Heart listener_Heart;
	private RecDataCallBackListener_Serach listener_Serach;
	private DisconnectListener listener_disconnect;
	private RecDataCallBackListener_Study listener_Study;
	private RecDataCallBackListener_TempHumity listener_TempHumity;
	// private RecDataCallBackListener_Record listener_Record;
	private RecDataCallBackListener_Update listener_Update;
	private RecDataCallBackListener_Version listener_Version;
	// private RecDataCallBackListener_Time listener_Time;
	private RecDataCallBackListener_Lock listener_Lock;
	private OnConnectSuccessListener listener_ConnectSuccess;
	public static String user_id;
	private String command;
	private LinkedList<String> queue_local = new LinkedList<String>();
	private String heartBeatCmd;
	private String tempCmd;
	private String updateCmd;
	private long sendHeartBeatTime;
	private long receiveHeartBeatTime;
	private boolean isNotReceiving = true;
	private int mSDKVersion;
	private String version = "";
	private String versionCmd;
	private DatagramSocket udpclient;
	private String sendStr;
	private int code;
	private double temp;
	private double humity;

	public String getVersion() {
		if (version.length() < 1) {
			return "无法获取";
		} else {

		}
		return version;
	}

	public String getId() {
		return deviceId;
	}

	public double getTemp() {
		return temp;
	}

	public void setTemp(double temp) {
		this.temp = temp;
	}

	public double getHumity() {
		return humity;
	}

	public void setHumity(double humity) {
		this.humity = humity;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public boolean isLocalSuccess() {
		return localSuccess;
	}

	public void setLocalSuccess(boolean localSuccess) {
		this.localSuccess = localSuccess;
	}

	public String getMac() {
		return mac;
	}

	public boolean isStop() {
		return stop;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	public long getSendHeartBeatTime() {
		return sendHeartBeatTime;
	}

	public void setSendHeartBeatTime(long sendHeartBeatTime) {
		// Log.i("wmy", "设置发送时间");
		this.sendHeartBeatTime = sendHeartBeatTime;
	}

	public long getReceiveHeartBeatTime() {
		return receiveHeartBeatTime;
	}

	public void setReceiveHeartBeatTime(long receiveHeartBeatTime) {
		// Log.i("wmy", "设置接收时间");
		this.receiveHeartBeatTime = receiveHeartBeatTime;
	}

	public boolean isNotReceiving() {
		return isNotReceiving;
	}

	public void setNotReceiving(boolean isNotReceiving) {
		this.isNotReceiving = isNotReceiving;
		if (isNotReceiving) {
			receiveHeartBeatTime = System.currentTimeMillis();
		}
	}

	public boolean isRemoteSuccess() {
		return remoteSuccess;
	}

	public void setRemoteSuccess(boolean remoteSuccess) {
		this.remoteSuccess = remoteSuccess;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	/**
	 * 强制升级 不管当前固件是否已经是最新版本 都发V999.220进行升级
	 */
	public void forceUpdate() {
		updateCmd = ConvertUtil.getInstance().convertUpdateCmd(deviceId, "V999.220");
		sendMqttData(updateCmd);
	}

	/**
	 * 常规升级 将服务器推送过来的版本拼接到指令里面去
	 */
	public void normalUpdate(String newVersion, String downloadUrl) {
		updateCmd = ConvertUtil.getInstance().convertNormalUpdateCmd(deviceId, newVersion, downloadUrl);
		sendMqttData(updateCmd);
	}

	public MySocket(WlanInfo wlanInfo, final Context c) {

		this.mac = wlanInfo.getMac();
		this.manufacturer = wlanInfo.getManufacturer();
		this.productId = wlanInfo.getProductId();
		this.status = wlanInfo.getStatus();
		this.deviceId = wlanInfo.getDeviceId();
		this.c = c;
		versionCmd = transVersionCmd(deviceId);
		tempCmd = ConvertUtil.getInstance().convertTempCmd(deviceId);
		updateCmd = ConvertUtil.getInstance().convertUpdateCmd(deviceId, version);
		mSDKVersion = DeviceManager.getInstance().getmSDKVersion();
		Log.e("wmy", "创建一个socket" + deviceId + " " + mac);
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				startLocalTcpClient(c);
				startCommandThread();
				startHeartBeat();
				startTempThread();
				sendHeartBeatTime = System.currentTimeMillis();
				receiveHeartBeatTime = System.currentTimeMillis();
			}
		}).start();
	}

	// 需要建立一个发送方法，如果发送失败了，则将发送失败的消息放入缓存队列，待连接成功后发送
	// 有些消息在发送时 界面已经做出了响应，如开关，所以需要保证消息是能够被发送的 要对发送进行检测，如果发送失败 会触发异常
	// 则检查触发异常时当前的消息是否已经发送成功，如不成功，待连接成功后重新发送
	// 该消息队列里的消息在5秒内自动清零

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	private void startLocalTcpClient(final Context c) {
		isStopByUser = false;
		new Thread() {
			public void run() {
				while (!stop) {
					// Log.i("wmy", "mysocket再循环一次");
					try {
						if (localSuccess) {
							// Log.i("wmy", "已连接 睡10秒");
							sleep(10000);
							continue;
						}
						if (SocketManager.getInstance().pairing) {
							// Log.i("wmy", "正在配置 睡10秒");
							sleep(10000);
							continue;
						}
						if (mac.equals("")) {
							// Log.i("wmy", "mac为空");
							break;
						}
						// Log.i("wmy", "mysocket" + 214 + "尝试连接"+tcpIp);
						if (tcpIp == null || tcpIp == "") {
							int max = 9999;
							int min = 1000;
							Random random = new Random();
							int s = random.nextInt(max) % (max - min + 1) + min;
							udpclient = new DatagramSocket(s);
							final String sendStr = ConvertUtil.getInstance().convertUDPCmd(mac.toUpperCase(),
									manufacturer, productId);
							byte[] sendBuf = sendStr.getBytes();
							InetAddress addr = InetAddress.getByName("255.255.255.255");
							final DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, addr, 3000);

							new Thread(new Runnable() {
								@Override
								public void run() {
									// TODO Auto-generated method stub
									int count = 0;
									while ((!localSuccess) && count < 20) {
										// // 发送数据
										try {
											if (udpclient != null) {
												// Log.i("udp", count + " 发送UDP=
												// " + sendStr);
												udpclient.send(sendPacket);
												udpclient.send(sendPacket);
												udpclient.send(sendPacket);
												Thread.sleep(1000);
											}
										} catch (InterruptedException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										} catch (IOException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
										}
										count++;
									}
									if (count == 20) {
										// Log.i("wmy", "连接不成功");
										stopLocalTcpClient1();
									}
								}
							}).start();
							boolean udpFlag = true;
							while (udpFlag) {
								byte[] recvBuf = new byte[2048];
								DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
								// 接收数据
								udpclient.receive(recvPacket);
								String recvStr = new String(recvPacket.getData(), 0, recvPacket.getLength());
								tcpIp = recvPacket.getAddress().toString().substring(1);
								tcpPort = parseTCPPort(recvStr.replace("\t", ""));
								// Log.i("wmy", tcpIp+" "+tcpPort);
								recvStr = "";
								socket_local = new Socket();
								if (tcpPort == 0) {
									tcpPort = 5000;
								}
								socket_local.connect(new InetSocketAddress(tcpIp, tcpPort), 5000);
								socket_local.setTcpNoDelay(true);
								in_local = socket_local.getInputStream();
								out_local = socket_local.getOutputStream();
								Log.i("wmy", mac + "= " + tcpIp + " " + socket_local.isConnected());
								udpclient.close();
								udpclient = null;
								localSuccess = true;
								checkVersion();
								if (listener_ConnectSuccess != null) {
									listener_ConnectSuccess.onReceiveData(mac);
								}
								receiveHeartBeatTime = System.currentTimeMillis();
								recvPacket = null;
								// tcpIp = "";
								// tcpPort = 0;
								recvBuf = null;
								BufferedReader br = null;
								br = new BufferedReader(new InputStreamReader(in_local, "ASCII"));
								while (socket_local.isConnected()) {
									if (br != null) {
										char[] cha = new char[2048];
										int len = br.read(cha);
										if (len < 2048 && !(len < 0 || len > cha.length)) {
											String str = new String(cha, 0, len);

											// 收到数据后，先进行解析，获取cmd值，如果属于什么类型的回调，就放入什么类型的接口
											if (str != null) {
												str.replace("\t", "");
												parseData(str);
											}
										}
									}
								}
							}
						} else {
							// Log.i("wmy", "已经获取ip和端口，直接重连");
							boolean udpFlag = true;
							while (udpFlag) {
								socket_local = new Socket();
								socket_local.connect(new InetSocketAddress(tcpIp, tcpPort), 5000);
								socket_local.setTcpNoDelay(true);
								in_local = socket_local.getInputStream();
								out_local = socket_local.getOutputStream();
								Log.i("wmy", mac + "= " + tcpIp + " " + socket_local.isConnected());
								// udpclient.close();
								// udpclient = null;
								localSuccess = true;
								checkVersion();
								if (listener_ConnectSuccess != null) {
									listener_ConnectSuccess.onReceiveData(mac);
								}
								receiveHeartBeatTime = System.currentTimeMillis();
								// tcpIp = "";
								// tcpPort = 0;
								BufferedReader br = null;
								br = new BufferedReader(new InputStreamReader(in_local, "ASCII"));
								while (socket_local.isConnected()) {
									if (br != null) {
										char[] cha = new char[2048];
										int len = br.read(cha);
										if (len < 2048 && !(len < 0 || len > cha.length)) {
											String str = new String(cha, 0, len);
											if (str != null) {
												str.replace("\t", "");
												parseData(str);
											}
										}
									}
								}
							}

						}
					} catch (UnknownHostException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Log.i("wmy", "socket " + mac + " 348局域网连接被中断错误");
						localSuccess = false;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						// Log.i("wmy", "socket " + mac + "
						// 352局域网连接被中断错误"+"tcpIp = "+tcpIp);
						localSuccess = false;
						stopLocalTcpClient1();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Log.i("wmy", "socket " + mac + " 358局域网连接被中断错误");
						localSuccess = false;
					}
					try {
						sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
						Log.i("wmy", "socket " + mac + " 365局域网连接被中断错误");
						localSuccess = false;
					}
				}

			};
		}.start();
	}

	private void checkVersion() {
		if (localSuccess) {
			if (socket_local.isConnected()) {
				// Log.i("wmy", "检查版本 "+versionCmd);
				queue_local.add(versionCmd);
			}
		} else {
			// sendRemoteData1(versionCmd);
		}
	}

	public void checkState(String checkCmd) {
		sendMqttData(checkCmd);
	}

	public void startHeartBeat() {
		// heartBeatCmd = transHeartCmd();
		heartBeatCmd = ConvertUtil.getInstance().convertHeartCmd();
		new Thread() {
			public void run() {
				while (!stop) {
					try {
						// Log.i("heartbeat", "isNotReceiving =
						// "+isNotReceiving+" localSuccess = "+localSuccess);
						if (isNotReceiving) {
							// sendMqttData(heartBeatCmd);
							if (localSuccess) {
								if (socket_local.isConnected()) {
									queue_local.add(heartBeatCmd);
									sendHeartBeatTime = System.currentTimeMillis();
									// Log.i
									if (sendHeartBeatTime - receiveHeartBeatTime > 10 * 1000) {
										Log.i("wmy", mac + "超时！！" + (sendHeartBeatTime - receiveHeartBeatTime));
										localSuccess = false;
										stopLocalTcpClient1();
									}
								}
								Thread.sleep(9 * 1000);
								// Thread.sleep(5 * 1000);
							} else {
								Thread.sleep(5 * 1000);
							}
						} else {
							Thread.sleep(5 * 1000);
						}
					} catch (Exception e) {
						// TODO: handle exception

					}
				}
			}
		}.start();
	}

	public void startTempThread() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (!stop) {
					try {
						if (isNotReceiving && DeviceManager.getInstance().isNeedToCheckTemp()) {
							queue_local.add(tempCmd);
							Thread.sleep(9 * 1000);
						} else {
							Thread.sleep(5 * 1000);
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		}).start();
	}

	public void startCommandThread() {
		new Thread() {
			public void run() {
				while (!stop) {
					try {
						if (queue_local.size() > 0) {
							// if (localSuccess) {

							command = queue_local.removeFirst();
							// Log.i("wmy", "delay" + command);
							if (localSuccess) {
								out_local.write(command.getBytes());
								out_local.flush();
							} else {
								// Log.i("wmy", "deviceId = " + deviceId + " " +
								// command);
								SocketManager.getInstance().sendRemoteData(deviceId, command);
							}
							// }
						}
						sleep(1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
		}.start();
	}

	public void sendCmdNeedRec(String cmd) {
		if (localSuccess) {
			if (socket_local.isConnected()) {
				try {
					out_local.write(cmd.getBytes());
					out_local.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					localSuccess = false;
				}
			}
		}
	}

	public void sendMqttData(final String cmd) {
		// Log.i("wmy", "设备"+deviceId+"连接状态"+localSuccess);
		if (localSuccess) {
			if (socket_local.isConnected()) {
				if (cmd != null && out_local != null) {
					if (mSDKVersion >= 23) {
						new Thread(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								try {
									Thread.sleep(100);
									Log.e("socket", deviceId + "发送cmd" + cmd);
									out_local.write(cmd.getBytes());
									out_local.flush();
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}).start();
					} else {
						try {
							// Log.e("send", "发送cmd" + cmd);
							out_local.write(cmd.getBytes());
							out_local.flush();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							localSuccess = false;
						}
					}
				}
			}
		} else {
			SocketManager.getInstance().sendRemoteData(deviceId, cmd);
		}
	}

	public void sendMqttDataDelay(String buffer) {
		queue_local.add(buffer);
	}

	public void stopLocalTcpClient() {
		try {
			stop = true;
			isStopByUser = true;
			if (in_local != null) {
				in_local.close();// 关闭本地输入流
			}
			if (out_local != null) {
				out_local.close(); // 关闭本地输出流
			}
			if (socket_local != null) {
				socket_local.close(); // 关闭本地socket连接
			}
			localSuccess = false; // 将连接状态改为未连接
			// isStopByUser = true;
			// stop = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void stopLocalTcpClient1() {
		try {
			if (udpclient != null) {
				udpclient.close();
				udpclient = null;
			}
			if (in_local != null) {
				in_local.close();// 关闭本地输入流
			}
			if (out_local != null) {
				out_local.close(); // 关闭本地输出流
			}
			if (socket_local != null) {
				socket_local.close(); // 关闭本地socket连接
			}
			localSuccess = false; // 将连接状态改为未连接
			// isStopByUser = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void parseHeartBeat(String data) {
		JSONObject jo = null;
		try {
			jo = new JSONObject(data);
			int cmd = jo.optInt("cmd");
			if (cmd == 20) {
				receiveHeartBeatTime = System.currentTimeMillis();
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String transVersionCmd(String deviceId) {
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20," + SocketManager.getInstance().user_id);
			jo.put("d", "10," + deviceId);
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 1);
			jo.put("g", "" + jo1 + "");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}

	private void parseData(String recData) {
		receiveHeartBeatTime = System.currentTimeMillis();
		JSONObject jo = null;
		JSONObject jo1 = null;
		try {
			// Log.i("wmy", "获取recData = " + recData);
			jo = new JSONObject(recData);
			String g = jo.optString("g");
			// Log.i("wmy", "获取g = " + g);
			jo1 = new JSONObject(g);
			int cmd = jo1.optInt("cmd");
			// Log.i("wmy", "获取cmd = " + cmd);
			// 心跳 搜索32 控制RF33 定时 红外64 学习65 温湿度48 记录 升级248 版本242 时间243 校准244
			switch (cmd) {
			case 21:
				code = jo.getInt("code");
				if (code == 0) {
					if (listener_Bind != null) {
						listener_Bind.onReceiveData(recData);
					}
				} else {

				}
				break;
			case 20:
				if (listener_Heart != null) {
					listener_Heart.onReceiveData(recData);
				}
				break;
			case 100:
				if (listener_Serach != null) {
//					Log.i("wmy", "收到设备状态返回"+recData);
					listener_Serach.onReceiveData(recData);
				}
				break;
			case 33:

				break;
			case 102:
				if (listener_Lock != null) {
					listener_Lock.onReceiveData(recData);
				}
				break;
			case 110:
				if (listener_TempHumity != null) {
					listener_TempHumity.onReceiveData(recData);
				}
//				Log.i("wmy", "data = " + recData);
				temp = jo1.getDouble("temperature");
				humity = jo1.getDouble("humidity");
				break;
			case 87:
				Log.i("wmy", "获取状态返回 = " + recData);
				break;
			case 121:
				if (listener_Study != null) {
					listener_Study.onReceiveData(recData);
				}
				break;
			case 6: // 升级
				if (listener_Update != null) {
					listener_Update.onReceiveData(recData);
				}
				break;
			case 1: // 版本号
				code = jo1.getInt("code");
				if (code == 0) {
					version = jo1.optString("version");
				} else {
					version = "未知版本";
				}
				break;
			default:
				break;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int parseTCPPort(String data) {
		// Log.i("wmy", "parseTCPPort 解析data = " + data);
		int port = 0;
		JSONObject jo = null;
		try {
			jo = new JSONObject(data);
			String g = jo.getString("g");
			JSONObject jo2 = new JSONObject(g);
			// Log.i("wmy", "parseTCPPort 解析jo2 = " + jo2.toString());
			port = jo2.getInt("tcp_port");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return port;
	}

	// public interface RecDataCallBackListener {
	// public void onReceiveData(String data);
	// }

	public interface RecDataCallBackListener_Serach {
		public void onReceiveData(String data);
	}

	public interface RecDataCallBackListener_Version {
		public void onReceiveData(String data);
	}

	public interface RecDataCallBackListener_Update {
		public void onReceiveData(String data);
	}

	public interface RecDataCallBackListener_Study {
		public void onReceiveData(String data);
	}

	public interface RecDataCallBackListener_TempHumity {
		public void onReceiveData(String data);
	}

	public interface RecDataCallBackListener_Alarm {
		public void onReceiveData(String data);
	}

	public interface RecDataCallBackListener_Record {
		public void onReceiveData(String data);
	}

	public interface RecDataCallBackListener_Time {
		public void onReceiveData(String data);
	}

	public interface RecDataCallBackListener_Bind {
		public void onReceiveData(String data);
	}

	public interface OnConnectSuccessListener {
		public void onReceiveData(String mac);
	}

	public interface RecDataCallBackListener_Heart {
		public void onReceiveData(String data);
	}

	public interface RecDataCallBackListener_Lock {
		public void onReceiveData(String data);
	}

	public interface DisconnectListener {
		public void onDisconnect();
	}

	// public void setRecDataCallBackListener(RecDataCallBackListener
	// recDataCallBackListener) {
	// this.listener1 = recDataCallBackListener;
	// }

	public void setOnDisconnectListener(DisconnectListener disconnectListener) {
		this.listener_disconnect = disconnectListener;
	}

	public void setOnConnectSuccessListener(OnConnectSuccessListener onConnectSuccessListener) {
		this.listener_ConnectSuccess = onConnectSuccessListener;
	}

	public void setRecDataCallBackListener_Bind(RecDataCallBackListener_Bind recDataCallBackListener_Bind) {
		this.listener_Bind = recDataCallBackListener_Bind;
	}

	public void setRecDataCallBackListener_Heart(RecDataCallBackListener_Heart recDataCallBackListener_Heart) {
		this.listener_Heart = recDataCallBackListener_Heart;
	}

	public void setRecDataCallBackListener_Serach(RecDataCallBackListener_Serach recDataCallBackListener_Serach) {
		this.listener_Serach = recDataCallBackListener_Serach;
	}

	public void setRecDataCallBackListener_Version(RecDataCallBackListener_Version recDataCallBackListener_Version) {
		this.listener_Version = recDataCallBackListener_Version;
	}

	public void setRecDataCallBackListener_Update(RecDataCallBackListener_Update recDataCallBackListener_Update) {
		this.listener_Update = recDataCallBackListener_Update;
	}

	public void setRecDataCallBackListener_Study(RecDataCallBackListener_Study recDataCallBackListener_Study) {
		this.listener_Study = recDataCallBackListener_Study;
	}

	public void setRecDataCallBackListener_TempHumity(
			RecDataCallBackListener_TempHumity recDataCallBackListener_TempHumity) {
		this.listener_TempHumity = recDataCallBackListener_TempHumity;
	}

	public void setRecDataCallBackListener_Lock(RecDataCallBackListener_Lock listener_Lock) {
		this.listener_Lock = listener_Lock;
	}

}
